<template>
	<view class="content">
		<u-tabs :list="orderValue" :scrollable="false" lineColor="#296844" @click="tabClick"></u-tabs>
		<view class="tip" v-if="orderList==''">
			<view>
				<text class="black-color big">还没有订单哦~</text>
			</view>

			<u-button class="buy-btn middle black-color" @click="buyClick">立即选购</u-button>
		</view>

		<view v-for="(item,index) in orderListIng" v-if="orderTag=='进行中'">
			<uni-card>
				<view class="card">
					<view class="item">
						<!-- 店铺名 -->
						<view class="store-pic">
							<image class="store-image" :src="item.store_logo"></image>
						</view>
						<text class="black-color big bold store-name ">{{item.store_name}}</text>
						<!-- 订单状态 -->
						<text class="middle card-status">{{item.status}}</text>
					</view>
					<view class="bag_num">
						<!-- 取袋号 -->
						<view>
							<text class="big black-color bag_text">取袋号:</text>
							<text class="big black-color bold">{{item.bag_num}}</text>
						</view>
						<!-- 取袋时间 -->
						<view>
							<text class="big black-color bag_text">取袋时间:</text>
							<text class="big black-color">{{item.start_time}}-{{item.end_time}}</text>
						</view>
					</view>
					<!-- 商品信息 -->
					<view class="good-item">
						<view class="good_title">
							<view class="good-pic">
								<image class="good-image" :src="item.good_logo"></image>
							</view>
							<text class="middle black-color good-name">{{item.good_name}}</text>
						</view>

						<text class="middle black-color">×{{item.buy_num}}</text>
						<text class="middle black-color">￥{{item.price*item.buy_num}}</text>
					</view>
					<!-- 订单时间 -->
					<view>
						<text class="middle">下单时间：{{item.buy_time | formatDate}}</text>
					</view>

				</view>
			</uni-card>
		</view>

		<view v-for="(item,index) in orderListDone" v-if="orderTag=='已完成'">
			<uni-card>
				<view class="card">
					<view class="item">
						<!-- 店铺名 -->
						<view class="store-pic">
							<image class="store-image" :src="item.store_logo"></image>
						</view>
						<text class="black-color big bold store-name ">{{item.store_name}}</text>
						<!-- 订单状态 -->
						<text class="middle card-status">{{item.status}}</text>
					</view>
					<view class="bag_num">
						<!-- 取袋号 -->
						<view>
							<text class="big black-color bag_text">取袋号:</text>
							<text class="big black-color bold">{{item.bag_num}}</text>
						</view>
					</view>
					<!-- 商品信息 -->
					<view class="good-item">
						<view class="good_title">
							<view class="good-pic">
								<image class="good-image" :src="item.good_logo"></image>
							</view>
							<text class="middle black-color good-name">{{item.good_name}}</text>
						</view>

						<text class="middle black-color">×{{item.buy_num}}</text>
						<text class="middle black-color">￥{{item.price*item.buy_num}}</text>
					</view>
					<!-- 订单时间 -->
					<view class="last">
						<text class="middle order-time">下单时间：{{item.buy_time | formatDate}}</text>
					</view>
					<view>
						<text class="middle order-time">完成时间：{{item.done_time | formatDate}}</text>
					</view>
					<!-- 评价按钮 -->
					<view v-if="item.score==0">
						<u-button class="ping-btn middle black-color" @click="rateClick($event,index)">立即评价</u-button>
					</view>
					<!-- 我的评价 -->
					<view v-if="item.score>0">
						<text class="middle order-time">我的评价：{{item.score}}分</text>
					</view>
				</view>
			</uni-card>

		</view>
		<view>
			<!-- 普通弹窗 -->
			<uni-popup ref="popup" background-color="#fff" @change="change">
				<view class="rate-box">
					<text class="big">请对此订单进行评分：</text>
					<uni-rate class="rate" allow-half :value="0" :size="40" @change="rateChange"/>
					<text class="cancel-btn big" @click="cancelClick()">取消</text>
					<text class="sub-btn big" @click="subClick()">提交</text>
				</view>
			</uni-popup>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderValue: [{
					name: '进行中'
				}, {
					name: '已完成'
				}],
				orderTag: '进行中',
				orderList: '',
				orderListIng: '',
				orderListDone: '',
				rateId: '',
				rate: 0,
			}
		},
		onLoad() {
			this.getOrder();
		},
		onShow() {
			this.getOrder();
		},
		onPullDownRefresh() {
			this.getOrder();
			//结束下拉刷新
			setTimeout(() => {
				uni.showToast({
					title: '刷新成功',
					icon: 'none'
				});
				uni.stopPullDownRefresh();
			}, 1000);

		},
		methods: {
			getOrder() {
				uniCloud.callFunction({
						name: 'getOrder',
						data: {
							'user_id': uni.getStorageSync('userId'),
							'status': this.orderTag,
						}
					})
					.then(res => {
						this.orderTag == '进行中' ? this.orderListIng = res.result.data : this.orderListDone = res.result
							.data;
						this.orderList = res.result.data;
					})
					.catch(error => {
						console.log(error);
					});
			},
			tabClick(e) {
				this.orderTag = this.orderValue[e.index].name;
				this.getOrder();
			},
			buyClick() {
				uni.switchTab({
					url: "../index/index"
				})
			},
			rateClick(e, index) {
				this.rateId = this.orderListDone[index]._id;
				this.$refs.popup.open('center');
			},
			rateChange(e){
				this.rate = e.value;
			},
			cancelClick(){
				this.$refs.popup.close('center');
			},
			subClick(){
				uni.showLoading({
					title:'提交中...'
				});
				uniCloud.callFunction({
						name: 'updateScore',
						data: {
							'_id': this.rateId,
							'score': this.rate,
						}
					})
					.then(res => {
						this.getOrder();
						uni.hideLoading();
						this.$refs.popup.close('center');
						uni.showToast({
							title:'评价成功',
							icon:'none'
						})
					})
					.catch(error => {
						console.log(error);
					});
			}
		},
		//时间戳的处理    
		filters: {
			formatDate: function(value) {
				let date = new Date(value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h; //小时补0
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m; //分钟补0
				let s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s; //秒补0
				return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s; //年月日时分秒
			}

		},
	}
</script>

<style>
	.small {
		font-size: 20rpx;
	}

	.middle {
		font-size: 25rpx;
	}

	.big {
		font-size: 30rpx;
	}

	.bold {
		font-weight: bold;
	}

	.green-color {
		color: #296844;
	}

	.black-color {
		color: black;
	}

	.red-color {
		color: brown;
	}

	.tip {
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 400rpx;
	}

	.buy-btn {
		width: 40%;
		border-radius: 40rpx;
		border: 1px solid #296844;
		background-color: rgba(123, 197, 111, 0.7);
		margin: 30rpx;
	}

	.card {
		display: flex;
		flex-direction: column;
	}

	.item {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: row;
	}

	.store-pic {
		flex: 1;
		margin-right: 10rpx;
	}

	.store-image {
		display: block;
		margin: 0 auto;
		width: 30px;
		height: 30px;
		border-radius: 5px;
	}

	.store-name {
		flex: 7;
	}

	.card-status {
		flex: 2;
		text-align: right;
	}

	.bag_num {
		margin: 20rpx 0;
		background-color: rgba(123, 197, 111, 0.3);
		padding: 20rpx;
		border-radius: 5px;
	}

	.bag_text {
		margin-right: 10rpx;
	}

	.good-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.good_title {
		display: flex;
		align-items: center;
		flex-direction: row;
	}

	.good-pic {
		flex: 1;
		margin-right: 20rpx;
	}

	.good-image {
		display: block;
		margin: 0 auto;
		width: 50px;
		height: 50px;
		border-radius: 5px;
	}

	.good-name {
		flex: 9;
	}

	.ping-btn {
		width: 40%;
		border-radius: 40rpx;
		border: 1px solid #296844;
		background-color: rgba(123, 197, 111, 0.7);
		float: right;
	}
	
	.rate-box{
		padding: 70rpx 90rpx;
	}
	
	.rate{
		margin: 30rpx 0;
	}
	
	.sub-btn {
		color: #2979ff;
		float: right;
		margin-bottom: 30rpx;
	}
	
	.cancel-btn {
		float: left;
		margin-bottom: 30rpx;
	}
</style>